#!/usr/bin/env python
# -*- coding: utf-8 -*-

from pymessagebus.default.messagebus import MessageBus
from pymessagebus.default.commandbus import CommandBus
import threading
from event import event_type
from event.handler import operation_handler
from command import command
from command.handler import user_handler

class MessageBusManager(object):
    """
    功能：单件模式消息总线。
    """

    __instance = None
    __message_bus = None
    __command_bus = None

    def __init__(self):
        pass

    def __new__(cls, *args, **kwargs):
        if not cls.__instance:
            cls.__instance = super(MessageBusManager, cls).__new__(cls, *args, **kwargs)
            cls.__message_bus = MessageBus()  # 全局Message Bus
            cls.__command_bus = CommandBus()  # 全局Command Bus

            # 订阅消息
            cls.__message_bus.add_handler(event_type.OperationLogEvent, operation_handler.handle_operation_creation)

            # 监听命令
            cls.__command_bus.add_handler(command.CreateUserCommand, user_handler.handle_user_creation)
        return cls.__instance

    def get_message_bus(self):
        return self.__message_bus

    def get_command_bus(self):
        return self.__command_bus
